package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        if (StringUtils.isNotBlank(setmealPageQueryDTO.getName())) {
            setmealPageQueryDTO.setName(setmealPageQueryDTO.getName().trim());
        }
        Page<SetmealVO> setmealVOS = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(setmealVOS.getTotal(), setmealVOS.getResult());
    }

    /**
     * 新增套餐数据
     * @param setmealDTO
     */
    @Override
    public void saveSetmealWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //添加套餐基本信息
        setmealMapper.save(setmeal);
        //获取套餐id
        Long setmealId = setmeal.getId();
        //获取套餐中的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //将每个菜设置成套餐id中
            setmealDish.setSetmealId(setmealId);
        }
        //批量添加菜品数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 回显套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealBySetmealId(Long id) {
        //获取套餐的基本信息
        SetmealVO setmealVO = setmealMapper.getSetmealBySetmealId(id);
        //获取菜品数据
        List<SetmealDish> dishList = setmealDishMapper.getDishBySetmealId(id);
        //将菜品设置到套餐中
        setmealVO.setSetmealDishes(dishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    public void updateWithDishs(SetmealDTO setmealDTO) {
        //创建套餐实体类
        Setmeal setmeal = new Setmeal();
        //拷贝前端传来的数据到实体类
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //修改基本套餐
        setmealMapper.update(setmeal);
        //删除菜品根据套餐id
        setmealDishMapper.deleteDishById(setmealDTO.getId());
        //获取套餐里面的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes.size() > 0 && setmealDishes != null) {
            for (SetmealDish setmealDish : setmealDishes) {
                //将每个新的菜品设置套餐id
                setmealDish.setSetmealId(setmealDTO.getId());
            }
            //批量添加菜品到套餐中
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        //获取套餐中菜品的状态
        List<SetmealDish> dishBySetmeal = setmealDishMapper.getDishBySetmealId(id);
        for (SetmealDish setmealDish : dishBySetmeal) {
            Long dishId = setmealDish.getDishId();
            DishVO dish = dishMapper.getDishById(dishId);
            Integer status1 = dish.getStatus();
            if (status1==0) {
                //未启售菜品，套餐不能启售
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否为启售  启售不能删
        for (Long id : ids) {
            SetmealVO setmealBySetmeal = setmealMapper.getSetmealBySetmealId(id);
            if (setmealBySetmeal.getStatus() == 1) {
                //启售套餐无法删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐数据
        setmealMapper.deleteBatch(ids);
        //删除套餐里面的菜品
        for (Long id : ids) {
                setmealDishMapper.deleteDishBySetmealId(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
